$(document).ready(function(){
	$("#btnFilter").click(function(){
		filterProjectBySelectedOption();
	});
	$("#bthAdd").click(function(){
		popupAddDialog();
	});

	filterProjectBySelectedOption();
	
	initDialog();
});

function filterProjectBySelectedOption() {
	filterProject("");
}

function initDialog() {
	$("#projectDialog #startDate").datepicker({
		  dateFormat: "yy-mm-dd"
	});
}


function filterProject(active) {
	var url = location.href + "/..";
	$("#txtMessage").text("页面正在加载...")
    $.ajax({
        type: "GET",
        url: url,
        data: {active: active},
        dataType: "json",
        success: function (resp) {
            if (resp.code == "0") {
                var tableBodyObj = $("#tableProjects tbody");
                tableBodyObj.html("");
                $.each(resp.data,function(index, item) {
                	var rowHtml = "<tr dataStr=" + JSON.stringify(item) + ">";
                	rowHtml += "<td>" + item.id + "</td>";
                	rowHtml += "<td>" + item.name + "</td>";
                	rowHtml += "<td>" + item.prefix + "</td>";
                	rowHtml += "<td>" + item.startDate + "</td>";
                	if(item.description.length>20) {
                		rowHtml += "<td>" + item.description.substring(0,17) + "...</td>";
                	} else {
                    	rowHtml += "<td>" + item.description + "</td>";
                	}
                	rowHtml += "<td>"
                	rowHtml += "<span class='clickable' onclick='popupEditDialog(event)'>edit</span>";
                	rowHtml += "<span class='clickable' onclick='showDetail(" + item.id + ")'>显示详情</span>";
                	rowHtml +="</td>"
                	rowHtml += "data-jsonStr='" + item + "'";
                	rowHtml += "</tr>";
                	tableBodyObj.append(rowHtml);
                });
                
                var countSummaryObj = $("#txtCountSummary");
                countSummaryObj.html("共 " + resp.data.length + " 条记录");
                
            } else {
                alert("服务器返回出错信息：\n\n" + resp.message);
            }
        	$("#txtMessage").text("");
        },
        error: function () { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });
}

var projectDialog = $("#projectDialog").dialog({
    autoOpen: false,
    height:320,
    width: 450,
    modal: true,
	close: function() {}
});

function popupAddDialog() {
    $("#projectDialog #id").val("");
    $("#projectDialog #name").val("");
    $("#projectDialog #prefix").val("");
    $("#projectDialog #startDate").val("");
    $("#projectDialog #description").val("");
    
	projectDialog = projectDialog.dialog({
		label: "增加新的项目",
	    buttons: {
	    	"添加新项目": addProject,
	    	Cancel: function() {
	    		projectDialog.dialog("close");
	    	}
	    }
	});
	projectDialog.dialog("open");
}

function addProject() {
	var name = $("#projectDialog #name").val();
    var prefix = $("#projectDialog #prefix").val();
    var startDate = $("#projectDialog #startDate").val();
    var description = $("#projectDialog #description").val();
	var postData = {name:name, prefix:prefix, startDate:startDate, description:description};
	var url = location.href + "/..";
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(postData),
        dataType: "json",
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	projectDialog.dialog("close");
            	filterProject(); // 刷新页面
                
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function() { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });    
}

function popupEditDialog(event) {
	event = event || window.event;
    var sourceObj = event.srcElement || event.target;
    var trObject = $(sourceObj).closest("tr");
    var item = JSON.parse(trObject.attr("dataStr"));
    
    $("#projectDialog #id").val(item.id);
    $("#projectDialog #name").val(item.name);
    $("#projectDialog #prefix").val(item.prefix);
    $("#projectDialog #startDate").val(item.startDate);
    $("#projectDialog #description").val(item.description);
    
	projectDialog = projectDialog.dialog({
		label: "修改项目基础信息",
	    buttons: {
	    	"保存修改内容": updateProject,
	    	Cancel: function() {
	    		projectDialog.dialog("close");
	    	}
	    }
	});
	projectDialog.dialog("open");

    
}

function updateProject() {
	var id = $("#projectDialog #id").val();
	var name = $("#projectDialog #name").val();
    var prefix = $("#projectDialog #prefix").val();
    var startDate = $("#projectDialog #startDate").val();
    var description = $("#projectDialog #description").val();
	var postData = {id:id, name:name, prefix:prefix, startDate:startDate, description:description};
	
    $.ajax({
        type: "POST",
        url: id,
        data: JSON.stringify(postData),
        dataType: "json",
        contentType:"application/json",
        success: function (resp) {
            if (resp.code == "0") {
            	projectDialog.dialog("close");
            	filterProject(); // 刷新页面               
            } else {
            	alert("服务器返回出错信息：\n\n" + resp.message);
            }
        },
        error: function () { alert("Sorry，服务器处理请求出错，请联系IT运维人员。"); }
    });
}

function showDetail(id) {
	window.location.href = "detail?id=" + id;
}
